package results

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

type Result struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func Success(c *gin.Context) {
	render(c, &Result{
		Code: 0,
		Msg:  "success",
		Data: nil,
	})
}
func Failure(c *gin.Context) {
	render(c, &Result{
		Code: -1,
		Msg:  "failure",
		Data: nil,
	})
}
func SuccessWithData(c *gin.Context, data interface{}) {
	render(c, &Result{
		Code: 0,
		Msg:  "success",
		Data: data,
	})
}
func FailureWithData(c *gin.Context, data interface{}) {
	render(c, &Result{
		Code: -1,
		Msg:  "failure",
		Data: data,
	})
}
func SuccessWithDataMsg(c *gin.Context, msg string, data interface{}) {
	render(c, &Result{
		Code: 0,
		Msg:  msg,
		Data: data,
	})
}
func FailureWithDataMsg(c *gin.Context, msg string, data interface{}) {
	render(c, &Result{
		Code: -1,
		Msg:  msg,
		Data: data,
	})
}
func SuccessWithFull(c *gin.Context, code int, msg string, data interface{}) {
	render(c, &Result{
		Code: code,
		Msg:  msg,
		Data: data,
	})
}
func FailureWithFull(c *gin.Context, code int, msg string, data interface{}) {
	render(c, &Result{
		Code: code,
		Msg:  msg,
		Data: data,
	})
}

func SuccessWithCodeMsg(c *gin.Context, code int, msg string) {
	render(c, &Result{
		Code: code,
		Msg:  msg,
		Data: nil,
	})
}
func FailureWithCodeMsg(c *gin.Context, code int, msg string) {
	render(c, &Result{
		Code: code,
		Msg:  msg,
		Data: nil,
	})

}
func render(c *gin.Context, result *Result) {
	c.JSON(http.StatusOK, gin.H{
		"code": result.Code,
		"msg":  result.Msg,
		"data": result.Data,
	})
}
